<template>
  <a-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">

    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="销售合同号">
          <a-input placeholder="请输入销售合同号" v-decorator="['contractNo', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="销售类型">
          <a-input placeholder="请输入销售类型" v-decorator="['sellingType', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="车架号">
          <a-input placeholder="请输入车架号" v-decorator="['vin', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="总价">
          <a-input-number v-decorator="[ 'finalPrice', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="交车地址">
          <a-input placeholder="请输入交车地址" v-decorator="['deliveryAddr', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="发票时间">
          <a-date-picker showTime format='YYYY-MM-DD HH:mm:ss' v-decorator="[ 'invoiceTime', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="交车时间">
          <a-date-picker showTime format='YYYY-MM-DD HH:mm:ss' v-decorator="[ 'deliveryTime', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="所属集团">
          <a-input placeholder="请输入所属集团" v-decorator="['groupName', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="经销商">
          <a-input placeholder="请输入经销商" v-decorator="['dealerName', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="销售">
          <a-input placeholder="请输入销售" v-decorator="['salesName', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="证件类型">
<!--          <a-select placeholder="请选择证件类型" v-decorator="['pinType', {}]" >-->
<!--            <a-select-option value="身份证">-->
<!--              身份证-->
<!--            </a-select-option>-->
<!--            <a-select-option value="士兵证">-->
<!--              士兵证-->
<!--            </a-select-option>-->
<!--            <a-select-option value="护照">-->
<!--              护照-->
<!--            </a-select-option>-->
<!--          </a-select>-->
          <a-input placeholder="请选择证件类型" v-decorator="['pinType', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="购车人身份证号">
          <a-input placeholder="请输入购车人身份证号" v-decorator="['purchaserPin', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="确认人">
          <a-input placeholder="请输入确认人" v-decorator="['confirmedName', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="确认时间">
          <a-date-picker showTime format='YYYY-MM-DD HH:mm:ss' v-decorator="[ 'confirmedTime', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="交易状态">
          <template >
            <a-radio-group v-decorator="['status',validatorRules.status]" placeholder="交易状态">
              <a-radio value="0">
                草稿
              </a-radio>
              <a-radio value="1">
                已确认
              </a-radio>
              <a-radio value="2">
                反馈中
              </a-radio>
              <a-radio value="9">
                合同取消
              </a-radio>
            </a-radio-group>
          </template>
        </a-form-item>

      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import { httpAction } from '@/api/manage'
import pick from 'lodash.pick'
import moment from "moment"

export default {
  name: "CxmTransactionModal",
  data () {
    return {
      title:"交易信息",
      visible: false,
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },

      confirmLoading: false,
      form: this.$form.createForm(this),
      validatorRules:{
      },
      url: {
        add: "/cxm/cxmTransaction/add",
        edit: "/cxm/cxmTransaction/edit",
      },
    }
  },
  created () {
  },
  methods: {
    add () {
      this.edit({});
    },
    edit (record) {
      this.form.resetFields();
      this.model = Object.assign({}, record);
      this.visible = true;
      this.$nextTick(() => {
        this.form.setFieldsValue(pick(this.model,'contractNo','sellingType','vin','finalPrice','deliveryAddr','groupId','groupName','dealerId','dealerName','salesId','salesName','pinType','purchaserPin','confirmedBy','confirmedName','status','createUser','updateUser'))
        //时间格式化
        this.form.setFieldsValue({deliveryTime:this.model.deliveryTime?moment(this.model.deliveryTime):null})
        this.form.setFieldsValue({invoiceTime:this.model.invoiceTime?moment(this.model.invoiceTime):null})
        this.form.setFieldsValue({confirmedTime:this.model.confirmedTime?moment(this.model.confirmedTime):null})
      });

    },
    close () {
      this.$emit('close');
      this.visible = false;
    },
    handleOk () {
      const that = this;
      // 触发表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
          that.confirmLoading = true;
          let httpurl = '';
          let method = '';
          if(!this.model.id){
            httpurl+=this.url.add;
            method = 'post';
          }else{
            httpurl+=this.url.edit;
            method = 'put';
          }
          let formData = Object.assign(this.model, values);
          //时间格式化
          formData.deliveryTime = formData.deliveryTime?formData.deliveryTime.format('YYYY-MM-DD HH:mm:ss'):null;
          formData.invoiceTime = formData.invoiceTime?formData.invoiceTime.format('YYYY-MM-DD HH:mm:ss'):null;
          formData.confirmedTime = formData.confirmedTime?formData.confirmedTime.format('YYYY-MM-DD HH:mm:ss'):null;

          console.log(formData)
          httpAction(httpurl,formData,method).then((res)=>{
            if(res.success){
              that.$message.success(res.message);
              that.$emit('ok');
            }else{
              that.$message.warning(res.message);
            }
          }).finally(() => {
            that.confirmLoading = false;
            that.close();
          })



        }
      })
    },
    handleCancel () {
      this.close()
    },


  }
}
</script>

<style lang="less" scoped>

</style>